package com.mach.platform.controller;

import com.mach.platform.domain.Response;
import com.mach.platform.domain.iot.IotConfigDevice;
import com.mach.platform.service.ConfigDeviceServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.controller
 * @time 2018/9/27 0027 13:06
 */
@RestController
@RequestMapping("condev")
public class ConfigDeviceCtrl extends BaseCtrl<IotConfigDevice> {

	@Autowired
	private ConfigDeviceServiceImpl configDeviceService;

	/**
	 * -- @GetMapping(value = "/info/{id}")
	 * -- @ApiOperation(value = "单个实体信息", notes = "接口备注：无", produces = "application/json")
	 * 单个数据 的接口
	 * -- @PathVariable(value = "id", required = true) Long id
	 *
	 * @param id 数据的id
	 * @return Response
	 */
	@Override
	public Response info (Long id) {
		return Response.ok(null);
	}

	/**
	 * --	@ApiOperation(value = "列表--分页", produces = "application/json", notes = "列表参数：size(每页数量),page(页码),searchParam(查询内容)")
	 * -- @GetMapping(value = "/list")
	 * 获取列表数据
	 * -- @PageableDefault(size = 25) Pageable pageable
	 * -- @RequestParam Map params
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Response
	 */
	@GetMapping(value = "/list")
	@Override
	public Response list (@PageableDefault(size = 25) Pageable pageable, @RequestParam Map params) {

		Page<IotConfigDevice> dbResult = configDeviceService.list(pageable, params);

		dbResult.getContent().forEach(item -> System.out.println(item));

		return Response.ok(dbResult);
	}

	/**
	 * -- @DeleteMapping("/del")
	 * -- @ApiOperation(value = "删除", notes = "接口备注：无", produces = "application/json")
	 * 删除数据
	 * -- @RequestBody Long[] ids
	 *
	 * @param ids id的集合
	 * @return Response
	 */
	@DeleteMapping("/del")
	@Override
	public Response delete (@RequestBody Long[] ids) {
		int delete = configDeviceService.delete(ids);
		return Response.ok("成功删除"+delete+"条数据");
	}

	/**
	 * --@PostMapping("/save")
	 * --	@ApiOperation(value = "保存", notes = "接口备注：无", produces = "application/json")
	 * 保存一个 对象
	 * --@RequestBody T t
	 *
	 * @param iotConfigDevice 保存的实体
	 * @return Response
	 */
	@PostMapping("/save")
	@Override
	public Response save (@RequestBody IotConfigDevice iotConfigDevice) {
		IotConfigDevice iotConfigDevice1 = configDeviceService.save(iotConfigDevice);
		return Response.ok(iotConfigDevice1);
	}

	/**
	 * --@PutMapping("/upd")
	 * --	@ApiOperation(value = "更新", notes = "接口备注：无", produces = "application/json")
	 * 更新一个对象
	 * --@RequestBody T t
	 *
	 * @param iotConfigDevice 更新的实体
	 * @return Response
	 */
	@PutMapping("/upd")
	@Override
	public Response update (@RequestBody IotConfigDevice iotConfigDevice) {
		IotConfigDevice iotConfigDevice1 = configDeviceService.update(iotConfigDevice);

		return Response.ok(iotConfigDevice1);
	}

	@PostMapping("slist")
	public Response saveList (@RequestBody List<IotConfigDevice> list) {
		System.out.println("参数is：" + list);
		if (list.size() == 0 ){
			return Response.error("未传有效数据");
		}

		int flag = configDeviceService.saveList(list);
		return Response.ok(flag);
	}
}
